/**
 * 
 */
package com.worksmap.core.task;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletContext;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.worksmap.core.constant.RateConstant;
import com.worksmap.core.entity.SellerConfig;
import com.worksmap.core.entity.TaoBaoSeller;
import com.worksmap.core.entity.Trade;
import com.worksmap.core.service.RateService;
import com.worksmap.core.service.TradeService;

/**
 * @author jack
 *
 */
public class RateTask implements Runnable {
	
	private RateService rateService;
	private TradeService tradeService;
	private TaoBaoSeller seller = null;
	private SellerConfig sellerConfig = null;
	private ServletContext servletContext = null;
	
	public RateTask(TaoBaoSeller seller, SellerConfig sellerConfig, ServletContext servletContext){
		this.seller = seller;
		this.sellerConfig = sellerConfig;
		this.servletContext = servletContext;
	}

	@Override
	public void run() {
		ApplicationContext application = WebApplicationContextUtils.getWebApplicationContext(servletContext);
		rateService = (RateService) application.getBean("rateService");
		tradeService = (TradeService) application.getBean("tradeService");
		List<Trade> tradeList = tradeService.getTradeBySellerConfig(seller, sellerConfig);
		if(sellerConfig.getBlack_list_switch() == RateConstant.BLACK_LIST_SWITCH_OPEN){
			List<Trade> tradeResultList = new ArrayList<Trade>();
			Set<String> blackNameSet = new HashSet<String>();
			//获取黑名单列表,放到hashSet中
			for(Trade trade : tradeList){
				if(!blackNameSet.contains(trade.getBuyer_nick())){
					tradeResultList.add(trade);
				}
			}
			rateService.rateTradeList(tradeResultList);
		}else{
			rateService.rateTradeList(tradeList);
		}
	}

}
